package ru.ssau.customerManagement.model.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.*;

/**
 * Author: illabefat
 * Time: 02.11.2014
 */
@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.FIELD)

@Entity
@Table(name = "CUSTOMERS")
@NamedQueries(
{
    @NamedQuery(name = "Customer.allCustomers", query = "SELECT c FROM Customer c"),
    @NamedQuery(name = "Customer.byName", query = "SELECT c FROM Customer c WHERE c.name like :name")
})
public class Customer implements Serializable{
    private static final long serialVersionUID = -3753624543305297128L;

    @XmlAttribute
    @Id
    @NotNull
    @Column(name = "ID")
    private Long id;
    
    @XmlAttribute
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "NAME")
    private String name;
    
    @XmlAttribute
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "DESCRIPTION")
    private String description;

    public Customer(){}
    public Customer(Long id) {
        this(id, "", "");
    }

    public Customer(Long id, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public Long getId() {
        return id;
    }
    public void setId(Long id) { this.id=id; }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Customer)) return false;

        Customer customer = (Customer) o;

        if (!id.equals(customer.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
    
    @XmlRootElement(name="customers")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class CustomersList
    {
        @XmlElement(name = "customer")
        private List<Customer> customers;
        
        public CustomersList(List<Customer> customers)
        {
            this.customers = customers;
        }
        
        public CustomersList()
        {
            this.customers = new ArrayList<Customer>();
        }

        public List<Customer> getCustomers() {
            return customers;
        }

        public void setCustomers(List<Customer> customers) {
            this.customers = customers;
        }
    }
}
